package com.cqemme.comm.utils;

import com.cqemme.comm.po.Result;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * JackSon转化工具类
 * ClassName:JackSonUtils
 * Description:
 * TODO
 * Company:EM ChongQing SCIENCE And TECHNOLOGY CO.LTD
 * date:2019-04-12
 * author:Tommy
 *
 * 
 * ******************************************************
 * HISTORY
 * 2019-04-12 add by Tommy
 * 
 *
 * @author 86156
 */
@SuppressWarnings({"ALL", "AlibabaConstantFieldShouldBeUpperCase"})
@Slf4j
public class JackSonUtils {
	private static final ObjectMapper ob=new ObjectMapper();

	public static JsonNode transJsonNode(String jsonStr) {
		JsonNode readValue = null;
		try {
			readValue = ob.readTree(jsonStr);
		} catch (JsonParseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (JsonMappingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return readValue;
	}
	
	public static ArrayNode transArrayNode(String jsonStr) {
		ArrayNode readValue = null;
		try {
			readValue = ob.readValue(jsonStr, ArrayNode.class);
		} catch (JsonParseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (JsonMappingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return readValue;
	}
	public static String beanToString(Object jsonStr) {
		String readValue = null;
		try {
			readValue = ob.writeValueAsString(jsonStr);
		} catch (JsonParseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (JsonMappingException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return readValue;
	}
	public static <T> T strToBean(String jsonStr, Class<T> cls) {
		T ret = null;
		ObjectMapper mapper=new ObjectMapper();
		try {
			ret = mapper.readValue(jsonStr, cls);
		} catch (Exception ex) {
			log.error(ex.getMessage()+"\r\n"+jsonStr);
		} finally {

		}

		return ret;
	}

	/**
	 *  正常请求， 获取data
	 * @param string
	 * @return
	 */
	public static JsonNode getResultSuccessData(String string){
		JsonNode jsonNode = JackSonUtils.transJsonNode(string);
		if(jsonNode.get("statusCode").asInt() == 200){
			String data = jsonNode.get("data").toString();
			if(StringUtils.isNotBlank(data)){
				return  JackSonUtils.transJsonNode(data);
			}
		}
		return  null;
	}

	public static ArrayNode getResultSuccessDataArrayNode(String string){
		JsonNode jsonNode = JackSonUtils.transJsonNode(string);
		if(jsonNode.get("statusCode").asInt() == 200){
			String data = jsonNode.get("data").toString();
			if(StringUtils.isNotBlank(data)){
				return  JackSonUtils.transArrayNode(data);
			}
		}
		return  null;
	}


	/**
	 * 获取错误结果直接返回
	 * @param string
	 * @return
	 */
	public static Result getFeignFail(String string){
		JsonNode jsonNode = JackSonUtils.transJsonNode(string);
		Result result = new Result();
		if(jsonNode.get("statusCode").asInt() != 200){
			//result.setStatusCode(jsonNode.get("statusCode").asInt());
			result = JackSonUtils.strToBean(string, Result.class);
		}
		return  result;
	}
	
}
